/********************************************************************
 * PROJECT: CSAIR ROST PROJECT.
 * SUPPLIER: FOREVER OPEN SOURCE SOFTWARE INC.
 ********************************************************************
 * FILE: AbstractGanttSettings.java
 * (C) Copyright Foliage Team 2013, All Rights Reserved.
 ********************************************************************/
package org.foliage.rcp.ui.preference;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.TimeZone;

import org.eclipse.swt.SWT;

@SuppressWarnings("unchecked")
public abstract class AbstractGanttSettings implements IGanttSettings {

	// ----------------------------------------------- 

	private GanttMeasure topMeasure;

	private GanttMeasure middleMeasure = new GanttMeasure(Calendar.DAY_OF_MONTH, 1, "MMM dd, ''HH", SWT.LEFT);

	private GanttMeasure bottomMeasure = new GanttMeasure(Calendar.HOUR_OF_DAY, 1, "HH:mm", SWT.LEFT);

	private TimeZone timeZone = TimeZone.getDefault();

	private int measureWidth = 60;

	// ----------------------------------------------- 

	@Override
	public GanttMeasure getHeaderMinimalMeasure() {
		List<GanttMeasure> measures = new ArrayList<GanttMeasure>();
		if (getHeaderTopMeasure() != null) {
			measures.add(getHeaderTopMeasure());
		}
		if (getHeaderMiddleMeasure() != null) {
			measures.add(getHeaderMiddleMeasure());
		}
		if (getHeaderBottomMeasure() != null) {
			measures.add(getHeaderBottomMeasure());
		}
		return Collections.min(measures);
	}

	@Override
	public int getGanttMeasureLevel() {
		int level = 0;
		level += getHeaderBottomMeasure() != null ? 1 : 0;
		level += getHeaderMiddleMeasure() != null ? 1 : 0;
		level += getHeaderTopMeasure() != null ? 1 : 0;
		return level;
	}

	@Override
	public double getGanttMeasureProportion(GanttMeasure measure) {
		return measure.getTimeInterval() / getHeaderMinimalMeasure().getTimeInterval();
	}

	@Override
	public long getMeasureEquivalent() {
		return getHeaderMinimalMeasure().getTimeInterval() / getGanttMeasureWidth();
	}

	// ----------------------------------------------- 

	public GanttMeasure getHeaderTopMeasure() {
		return topMeasure;
	}

	public void setHeaderTopMeasure(GanttMeasure topMeasure) {
		this.topMeasure = topMeasure;
	}

	public GanttMeasure getHeaderMiddleMeasure() {
		return middleMeasure;
	}

	public void setHeaderMiddleMeasure(GanttMeasure middleMeasure) {
		this.middleMeasure = middleMeasure;
	}

	public GanttMeasure getHeaderBottomMeasure() {
		return bottomMeasure;
	}

	public void setHeaderBottomMeasure(GanttMeasure bottomMeasure) {
		this.bottomMeasure = bottomMeasure;
	}

	@Override
	public TimeZone getTimeZone() {
		return timeZone;
	}

	@Override
	public void setTimeZone(TimeZone timeZone) {
		this.timeZone = timeZone;
	}

	@Override
	public int getGanttMeasureWidth() {
		return measureWidth;
	}

	@Override
	public void setGanttMeasureWidth(int measureWidth) {
		this.measureWidth = measureWidth;
	}

}
